package com.example.android60permission;

import android.Manifest;
import android.app.ActivityOptions;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import okhttp3.Call;


/**
 * 1、结合BaseActivity，将权限控制放在Base里面
 * 2、结合PermissionGen开源，来控制权限
 * 3、结合RxPermission开源，来控制权限
 */
//读写sd卡
public class MainActivity extends AppCompatActivity {


    @Bind(R.id.button)
    Button button;
    @Bind(R.id.button2)
    Button button2;
    @Bind(R.id.button3)
    Button button3;
    @Bind(R.id.imgTest)
    ImageView imgTest;

    private RxPermissions rxPermissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        rxPermissions = new RxPermissions(this);
    }

    //http://images.csdn.net/20150817/1.jpg
    private void downloadFile() {
        //1
//        if(hasPermisson(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
//            doSdCardPermission();
//        }else {
//            requestPermissionsCustom(0,Manifest.permission.WRITE_EXTERNAL_STORAGE);
//        }

        //2
//        PermissionGen.with(this)
//                .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                .addRequestCode(200)
//                .request();
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        Log.e("...111", permission.name + "...");
                        switch (permission.name) {
                            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
                                if (permission.granted) {
                                    doSdCardPermission();
                                } else {
                                    Log.e("error_call", "Fail_Call_Phone");
                                }
                                break;

                            case Manifest.permission.READ_EXTERNAL_STORAGE:
                                if (permission.granted) {
                                    Log.e("okk_call", "okk_Call_Phone");
                                } else {
                                    Log.e("okk_error_call", "Fail_Call_Phone");
                                }
                                break;
                        }

                    }
                });
    }

    private void callPhone() {
        //1
//        if(hasPermisson(Manifest.permission.CALL_PHONE)){
//            doCallPhonePermission();
//        }else{
//            requestPermissionsCustom(1,Manifest.permission.CALL_PHONE);
//        }

        //2
//        PermissionGen.with(this)
//                .addRequestCode(100)
//                .permissions(Manifest.permission.CALL_PHONE)
//                .request();

        //3
        rxPermissions.requestEach(Manifest.permission.CALL_PHONE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        Log.e("...", permission.name + "...");
                        if (permission.granted) {
                            doCallPhonePermission();
                        } else {
                            Log.e("error_call", "Fail_Call_Phone");
                        }
                    }
                });

    }

//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        PermissionGen.onRequestPermissionsResult(this,requestCode,permissions,grantResults);
//    }

    //    @PermissionSuccess(requestCode = 100)
    public void doCallPhonePermission() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "10086");
        intent.setData(data);
        startActivity(intent);
    }

    //    @PermissionFail(requestCode = 100)
    public void doCallPhoneFail() {
        Log.e("error_call", "Fail_Call_Phone");
    }


    //    @PermissionSuccess(requestCode = 200)
    public void doSdCardPermission() {
        OkHttpUtils
                .get()
                .url("http://images.csdn.net/20150817/1.jpg")
                .build()
                .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "test_img.jpg") {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("testError", "onError :" + e.getMessage());
                    }

                    @Override
                    public void onResponse(File response, int id) {
                        Log.e("testSuccess", "onResponse :" + response.getAbsolutePath());
                    }
                });
    }

    //    @PermissionFail(requestCode = 200)
    public void doSdCardFail() {
        Log.e("error_sdcard", "Fail_sdcard");
    }

    @OnClick({R.id.button, R.id.button2,R.id.button3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button:
                callPhone();
                break;
            case R.id.button2:
                downloadFile();
                break;

            case R.id.button3:
                EasyTransitionOptions options =
                        EasyTransitionOptions.makeTransitionOptions(MainActivity.this,
                                button3,imgTest);
                Intent intent = new Intent(MainActivity.this,TwoActivity.class);
                EasyTransition.startActivity(intent,options);
                break;
        }
    }
}
